package com.duguo.dynamicmvc.factory;

import java.util.HashMap;
import java.util.Map;

import javassist.CtClass;
import javassist.CtMethod;

public class WorkingModel {


    private CtClass controllerCtClass;
    private CtMethod controllerHandlerCtMethod;
    private CtClass internalHandlerInterfaceCtClass;
    private boolean singleActionController = false;
    private boolean multiActionController = true;
    private boolean dynamicController = false;
    private String dynamicControllerName;
    private String controllerPath;

    private String controllerInterfaceName;
    private Class controllerClass;
    private Map<String, Object> staticViewMapping = new HashMap<String, Object>(0);
    private Map<String,Object> parameterResolvers;
    private Map<String,Object> parameterValidators;
    private Map registedControllers = new HashMap(0);

    private ControllerFactoryImpl controllerFactory;
    private WorkingModel parentWorkModel;
    private ControllerBuilder controllerBuilder;

    public WorkingModel(ControllerFactory controllerFactory) {
        this.controllerFactory = (ControllerFactoryImpl) controllerFactory;
    }

    // modified getter and setters


    public Object getControllerInstance() throws Exception {
        return controllerFactory.getObject();
    }

    public void setControllerInstance(Object controllerInstance) {
        controllerFactory.setControllerInstance(controllerInstance);
    }

    public Class getControllerTypeClass() {
        return controllerFactory.getObjectType();
    }

    public Class getParentControllerInterface() {
        if (parentWorkModel != null) {
            CtClass tempCtClass = parentWorkModel.getInternalHandlerInterfaceCtClass();
            try {
                return Class.forName(tempCtClass.getName());
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        return controllerFactory.getObjectType();
    }

    //genereal getter and setters

    public void setParentWorkModel(WorkingModel parentWorkModel) {
        this.parentWorkModel = parentWorkModel;
    }

    public Class getControllerClass() {
        return controllerClass;
    }

    public void setControllerClass(Class controllerClass) {
        this.controllerClass = controllerClass;
    }
    
    public String getControllerInterfaceName() {
        return controllerInterfaceName;
    }

    public void setControllerInterfaceName(String controllerInterfaceName) {
        this.controllerInterfaceName = controllerInterfaceName;
    }
    
    public Map<String, Object> getStaticViewMapping() {
        return staticViewMapping;
    }

    public void setStaticViewMapping(Map<String, Object> staticViewMapping) {
        this.staticViewMapping = staticViewMapping;
    }

    public CtClass getControllerCtClass() {
        return controllerCtClass;
    }

    public void setControllerCtClass(CtClass controllerCtClass) {
        this.controllerCtClass = controllerCtClass;
    }

    public CtMethod getControllerHandlerCtMethod() {
        return controllerHandlerCtMethod;
    }

    public void setControllerHandlerCtMethod(CtMethod controllerHandlerCtMethod) {
        this.controllerHandlerCtMethod = controllerHandlerCtMethod;
    }

    public CtClass getInternalHandlerInterfaceCtClass() {
        return internalHandlerInterfaceCtClass;
    }

    public void setInternalHandlerInterfaceCtClass(
            CtClass internalHandlerInterfaceCtClass) {
        this.internalHandlerInterfaceCtClass = internalHandlerInterfaceCtClass;
    }

    
    public boolean isSingleActionController() {
		return singleActionController;
	}

	public void setSingleActionController(boolean singleActionController) {
		this.singleActionController = singleActionController;
	}

	public boolean isMultiActionController() {
        return multiActionController;
    }

	public boolean isHandlerController() {
        return !multiActionController && !singleActionController;
    }

    public void setMultiActionController(boolean multiActionController) {
        this.multiActionController = multiActionController;
    }

    public Map getRegistedControllers() {
        return registedControllers;
    }

    public void setRegistedControllers(Map registedControllers) {
        this.registedControllers = registedControllers;
    }

    public String getDynamicControllerName() {
        return dynamicControllerName;
    }

    public void setDynamicControllerName(String dynamicControllerName) {
        this.dynamicControllerName = dynamicControllerName;
    }

    public boolean isDynamicController() {
        return dynamicController;
    }

    public boolean isRootController() {
        return getControllerBuilder().getProcessorHolder().getControllerProcessor().getControllerNameBuilder().isRootController(controllerPath);
    }
    

    public void setDynamicController(boolean dynamicController) {
        this.dynamicController = dynamicController;
    }

    public ControllerFactory getControllerFactory() {
        return controllerFactory;
    }

    public void setControllerFactory(ControllerFactoryImpl controllerFactory) {
        this.controllerFactory = controllerFactory;
    }

    public WorkingModel getParentWorkModel() {
        return parentWorkModel;
    }

    public ControllerBuilder getControllerBuilder() {
        return controllerBuilder;
    }

    public void setControllerBuilder(ControllerBuilder controllerBuilder) {
        this.controllerBuilder = controllerBuilder;
    }

    public String getControllerPath() {
        return controllerPath;
    }

    public void setControllerPath(String controllerPath) {
        this.controllerPath = controllerPath;
    }

	public Map<String, Object> getParameterResolvers() {
		return parameterResolvers;
	}

	public void setParameterResolvers(
			Map<String, Object> parameterResolvers) {
		this.parameterResolvers = parameterResolvers;
	}

	public Map<String, Object> getParameterValidators() {
		return parameterValidators;
	}

	public void setParameterValidators(Map<String, Object> parameterValidators) {
		this.parameterValidators = parameterValidators;
	}


}
